/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","ajaxServ","sysmanMain", "bizProto","btnServ","flowServ"],
		function($,datagridServ,ajaxServ, sysmanMain, bizProto,btnServ,flowServ) {

			'use strict';

			var _bizProto,_moduleConfig,_buttonConfig,_defaultConfig,
				//锚记datagrid目标元素
				record,
				//编辑弹窗对象及弹窗按钮对象
				$edit_window,$edit_window_height=700,$edit_window_width=700,window_btn,
				mainUrl = '/medicalOutpatient/query/getListDomain.action',
				itemUrl = '/medicalOutpatientItem/query/getListDomain.action',
				feeUrl = '/medicalOutpatientFee/query/getListDomain.action',
				//流程执行信息grid
				_historicGrid,
				//业务表单对象
				$form,_defaultConfig,
				_moduCode = 'outpatientItemFee'
				
				
				
			var public_method = {				
				
				init:function(_options){
					
					_defaultConfig ={
						doc:null,
						rootId:null,				
						panelSelector:'div.well',
						subSystemCode:null,
						moduleCode:null,
						editWindow:null,
						btnSelector:'div.flow-edit-btn',
						initQueryParams:null,
						initElement:null,
						historicUrl:null,
						historicField:null
					}
					_moduleConfig = sysmanMain.getModuConf(_moduCode);
					if (window.console) {
						console.info(_moduCode + ' modules gets ready and been loaded.');
					}
					
					
					_defaultConfig = $.extend({},_defaultConfig,_moduleConfig,_options)			
					_bizProto = new bizProto(_defaultConfig);			
					_bizProto.gridHeight -= 15
					
					_moduleConfig.bizProto=_bizProto
					_moduleConfig.defaultConfig = _defaultConfig
					
					
					
					if(_defaultConfig.editWindow){
						$edit_window = $('#'+_defaultConfig.editWindow)
						_moduleConfig.edit_window = _defaultConfig.editWindow
					}
					
					_buttonConfig={										    
						 "查看项目":private_method.queryItem,
						 "查看费用":private_method.queryFee,
						 "取消":private_method.cancel						 
					}
					
					_moduleConfig.buttonConfig = _buttonConfig
					//保存模块初始化信息
					sysmanMain.setModuConf(_moduCode,_moduleConfig)
					if(_defaultConfig.initQueryParams){
						
					}
					if(_defaultConfig.initQueryParams)
						ajaxServ.runAjax({
							url:_moduleConfig.bizProto.rootPath + mainUrl,
							contentType:'form',
							data:_defaultConfig.initQueryParams,
							callback:private_method.init})
										
				}
				
			}

			var private_method = {
				
				//初始化元素
				init : function(data) {
					record = data
					// 编辑窗口初始化									
					$form = $('#'+_defaultConfig.rootId + ' form.main-form')
					$form.form('clear')
					
					var _curRow
					if(record && record.rows && record.rows[0]){
						_curRow = record.rows[0]
						
						//患者性别
						if(_curRow.patient_sex==1){
							_curRow.sex = '男'
						}else{
							_curRow.sex = '女'
						}
						var _url = '/'+sysmanMain.getRootPath()+'/medicalOutpatientDiagnosis/query/getDiagnosisList.action';
						
						
						if(_curRow.patient_birth && _curRow.feeat){
							var bir = _curRow.patient_birth.split('-')
							var fee = _curRow.feeat.split('-')
							_curRow.age=_curRow.patient_birth +'  ,年龄: '+(fee[0]-bir[0])+' 周岁';
						}
						//判断是否存在诊断信息
						if(!_curRow.diagnosis_name){
							$.ajax({
								url   : _url,
								data  : {jzxh:_curRow.jzxh},
								async : false,
								success:function(data){
									var _diag_name = '';
									if(data && data.length>0){
										$.each(data,function(i,o){
											if(o && o.diagnosis){
												_diag_name = !_diag_name? o.diagnosis : _diag_name + ',' + o.diagnosis
											}
										})
										_curRow.diagnosis_name = _diag_name;
									}
								},
								error:function(){
									
								}
							})
						}
					}
					$form.form('load',_curRow)
					
					//绑定按钮事件					
					private_method.bindBtnEvent();					
					
					//加载流程执行信息
									
				},
				
				//绑定按钮事件
				bindBtnEvent:function(){
					
					window_btn = $($edit_window).find('a.function-btn')
					$(window_btn).unbind('click')
					$(window_btn).bind('click',	private_method.function_btn_listenner)
					
				},				
				
				// 功能按钮监听函数
				function_btn_listenner : function(e) {

					btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

				},
				
				queryItem:function(){
					_moduleConfig = sysmanMain.getModuConf(_moduCode);
					$('#outpatient-item-fee div.fee-panel').panel('close')
					var _itemPanel = $('#outpatient-item-fee div.item-panel')
					_itemPanel.panel('open')
					var $grid  =$('#outpatient-item-fee table.item-list')
					var _options = $($grid).datagrid('options')
					var _condition = _moduleConfig.initQueryParams.condition
					_condition = $.extend(_condition, {prescribe_code_like:'cf'})
					if(_options && _options.url){
						$grid.datagrid('load',{condition:_condition})
					}else{
						var _initParam = {
							url:_bizProto.rootPath + itemUrl,
							queryParams:{condition:_condition}
						}
						
						$grid.datagrid(_initParam)
					}
				},
				
				queryFee:function(){
					_moduleConfig = sysmanMain.getModuConf(_moduCode);
					$('#outpatient-item-fee div.item-panel').panel('close')
					
					var _feePanel = $('#outpatient-item-fee div.fee-panel')
					_feePanel.panel('open')
					var $grid  =$('#outpatient-item-fee table.fee-list')
					var _options = $($grid).datagrid('options')
					var _param ={prescribe_code : record.prescribe_code}
					var _condition = _moduleConfig.initQueryParams.condition
							_condition = $.extend(_condition, {perscribe_code_like:'cf'})
					if(_options && _options.url){
						$grid.datagrid('load',{condition:_condition})
					}else{
						var _initParam = {
							url:_bizProto.rootPath + feeUrl,
							queryParams:{condition:_condition}
						}
						
						$grid.datagrid(_initParam)
					}
				},
				//
				cancel : function() {
					$($edit_window).window('close');
				},
				initItem:function(_options){
					
				}
				

			};
			
			return public_method;

		});